﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using Akiha.Core;
using ESRI.ArcGIS.Geometry;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using System.Windows.Forms;


namespace GIS.GeoDataManage.Dialogs
{


    class GridControlExtensionForAO:GridControl
    {
        private IMap m_Map = null;
        private GridView gridView1 = null;
        private DataTable m_DT=null;
        private BindingSource m_BindingSource = new BindingSource();

        //private ITable m_pTable = null;

        public IMap Map { get { return m_Map; } set { m_Map = value; } }

        //扩展的构造函数
        public GridControlExtensionForAO(IMap pMap)
            :this()
        {
            m_Map = pMap;
        }

        //默认的构造函数
        public GridControlExtensionForAO()
        {
            gridView1 = (this.MainView) as GridView;
        }

        
        /// <summary>
        /// 显示字段
        /// </summary>
        /// <param name="pTable">传入的ITable</param>
        /// <param name="isFilterField">是否过滤字段</param>
        public void ShowFields(ITable pTable, bool isFilterField)
        {
            if (pTable != null)
            {
                //释放前一个dt资源，以节省内存资源
                
                this.DataSource = null;

                if (m_DT != null)
                {
                    m_DT.Dispose();
                    m_DT = null;
                }
                m_DT = DataAdaptorForAO.ITableToFieldsDT(pTable);

                //清除前一个gridView的视图
                ((GridView)this.MainView).Columns.Clear();
                this.DataSource = m_DT;
                if (isFilterField)
                {
                    //CheckField2();
                }

            }
        }

        /// <summary>
        /// 显示要素List
        /// </summary>
        /// <param name="lstFea">传入LstFea</param>
        /// <param name="isFilterField">是否隐藏不需显示的字段</param>

        public void ShowFeatures(List<IFeature> lstFea, bool isFilterField)
        {
            if (lstFea == null || lstFea.Count == 0)
            {
                return;
            }
            //释放前一个dt资源，以节省内存资源
            this.DataSource = null;
            if (m_DT != null)
            {
                m_DT.Dispose();
                m_DT = null;
            }
            m_DT = DataAdaptorForAO.ListFeaToDataTable(lstFea);
            this.DataSource = m_DT;
            if (isFilterField)
            {
                CheckField();
            }
        }

        //初始显示ITable的要素
        public void ShowTable(ITable pTable, bool isFilterField)
        {
            if (pTable != null)
            {
                //释放前一个dt资源，以节省内存资源
                this.DataSource = null;
                if (m_DT != null)
                {
                    m_DT.Dispose();
                    m_DT = null;
                }
                m_DT = DataAdaptorForAO.ITableToDataTable(pTable);
                this.DataSource = m_DT;
                if (isFilterField)
                {
                    CheckField();
                }
            }
        }



        //显示经过ITable数据绑定后的Data，能够插入、删除、编辑等等。
        //这样避免了转换，直接读取原始的Itable数据，显示性能更好。
        public void ShowTableEx(ITable pTable, bool isFilterField)
        {
            if (pTable != null)
            {
                m_BindingSource.DataSource = new TableWrapper(pTable);
                this.DataSource = m_BindingSource;
                //修改datasource后重新填充gridview的列
               
                ((GridView)this.MainView).OptionsView.ColumnAutoWidth = false;
                ((GridView)this.MainView).PopulateColumns();
            }
        }


        //显示多个Ifeature，且绑定到ITable
        public void ShowLstFeaturesEx(List<IRow> lstFeas, bool isFilterField)
        {
            if (lstFeas.Count != 0)
            {
                m_BindingSource.DataSource = new TableWrapper(lstFeas);
                this.DataSource = m_BindingSource;
                //修改datasource后重新填充gridview的列
                gridView1.OptionsView.ColumnAutoWidth = false;
                gridView1.PopulateColumns();
            }
            else
            {
                m_BindingSource.DataSource = null;
                this.DataSource = m_BindingSource;
                gridView1.PopulateColumns();
            }

        }



        //显示结果ITable绑定的字段Data
        //能够增删修改字段
        public void ShowFieldsEx(ITable pTable,bool isFilterFields)
        {
            if (pTable!=null)
            {
                m_BindingSource.DataSource = new FieldsWrapper(pTable);
                this.DataSource = m_BindingSource;
                //修改datasource后重新填充gridview的列
                gridView1.OptionsView.ColumnAutoWidth = false;
                gridView1.PopulateColumns();
            }
        }



        //对字段进行过滤显示
        //在dgv中隐藏掉
        private void CheckField()
        {

            //筛选不需要显示的字段
            string strField = "";
            foreach (GridColumn item in ((GridView)this.MainView).Columns)
            {
                strField = item.Name;
                if (strField == "Shape" || strField == "SHAPE.AREA" || strField == "SHAPE.LEN" || strField == "GUID")
                    item.Visible = false;
            }



            //oid/fid需要显示,shape,shapeArea,shapeLength





        }

        private void InitializeComponent()
        {
            this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // gridView1
            // 
            this.gridView1.GridControl = this;
            this.gridView1.Name = "gridView1";
            // 
            // GridControlExtensionForAO
            // 
            this.MainView = this.gridView1;
            this.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
            this.gridView1});
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }


        //private void CheckField2()
        //{
        //    string strField = "";
        //    string strType = "";
        //    strType = m_DT.Rows[0][0].ToString();

        //    //默认隐藏第一行fid或者objectid
        //    //gridControl1.Rows[0].Visible = false;

        //    foreach (DataGridViewRow r in gridView1.getc)
        //    {
        //        strField = r.Cells["Name"].Value.ToString();

        //        if (strType == "OBJECTID")
        //        {
        //            if (strField == "SHAPE" || strField == "SHAPE_Length" || strField == "SHAPE_Area" || strField == "GUID")
        //            {
        //                r.Visible = false;
        //            }

        //        }
        //        else if (strType == "FID")
        //        {
        //            if (strField == "Shape" || strField == "Id")
        //            {
        //                r.Visible = false;
        //            }
        //        }




        //    }
        //}




    }
}
